<?php
	function calculate_items($cart)
	{
		// sum total items in shopping cart
  		$items = 0;
		if(is_array($cart)) {
    		foreach($cart as $gameTitle => $qty) {
    			$items += $qty;
    		}
  		}
 		return $items;
	}
	
	function calculate_price($cart) 
	{
  		// sum total price for all items in shopping cart
  		$price = 0.0;
 		if(is_array($cart)) {
 			$conn = getConnection();
    		foreach($cart as $gameTitle => $qty) {
    			$query = "SELECT Price FROM 2011_Games WHERE Title = '$gameTitle'";
      			$result = $conn->query($query);
      			if ($result) {
        			$item = $result->fetch_object();
       				$item_price = $item->Price;
        			$price += $item_price*$qty;
     		 	}
    		}
		}
		$conn->close();
  		return $price;
	}
	
	function display_cart($cart, $change = true, $images = 1) 
	{
		$conn = getConnection();
		foreach ($cart as $title => $qty)
		{
			$query = "SELECT * FROM 2011_Games WHERE Title = '$title'";
			$result = $conn->query($query);
			if ($result)
			{
				$game = $result->fetch_assoc();
				echo '<div id="orders"><ul>
						<li id="ordersImg"><img src="'.$game['Image_url'].'" /></li>
						<li id="ordersTitle">'.$game['Title'].'</li>
						<li>Price: $'.$game['Price'].'</li>
						<li>Quantity: <form method="post"><input type="text" name="qtyEdit" style="width:20px" value="'.$qty.'">
							<input type="hidden" name="editTitle" value="'.$game['Title'].'">
							<input type="submit" value="Change"></form>
					  </ul></div>';
			}
		}
		$conn->close();
	}
?>